﻿// <copyright file="Community.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace MediaRss
{
    #region

    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.Serialization;

    #endregion

    [XmlRoot(ElementName = "community", Namespace = "http://search.yahoo.com/mrss/")]
    public class Community : MediaRssBase, IXmlSerializable
    {
        #region Constants and Fields

        internal const string ELEMENT_NAME = "community";

        #endregion

        #region Public Properties

        public StarRating Rating { get; set; }

        public Statistics Stats { get; set; }

        public Tags TagItems { get; set; }

        #endregion

        #region Public Methods

        public void ReadXml(XmlReader reader)
        {
            bool isEmptyElement = reader.IsEmptyElement;
            base.AddAttributeExtensions(reader);
            reader.ReadStartElement();
            if (!isEmptyElement)
            {
                while (reader.IsStartElement())
                {
                    string localName = reader.LocalName;
                    if (localName == null)
                    {
                        goto Label_00AA;
                    }
                    if (!(localName == "starRating"))
                    {
                        if (localName == "statistics")
                        {
                            goto Label_0074;
                        }
                        if (localName == "tags")
                        {
                            goto Label_008F;
                        }
                        goto Label_00AA;
                    }
                    this.Rating = new StarRating();
                    this.Rating.ReadXml(reader);
                    goto Label_00C3;
                    Label_0074:
                    this.Stats = new Statistics();
                    this.Stats.ReadXml(reader);
                    goto Label_00C3;
                    Label_008F:
                    this.TagItems = new Tags();
                    this.TagItems.ReadXml(reader);
                    goto Label_00C3;
                    Label_00AA:
                    base.ElementExtensions.Add((XElement)XNode.ReadFrom(reader));
                    Label_00C3:
                    ;
                }
            }
            reader.ReadEndElement();
        }

        public override string ToString()
        {
            var builder = new StringBuilder();
            builder.AppendFormat("StarRating: {0}\n", this.Rating);
            builder.AppendFormat("Statistics: {0}\n", this.Stats);
            builder.AppendFormat("Tags: {0}\n", this.TagItems);
            return builder.ToString();
        }

        public void WriteXml(XmlWriter writer)
        {
            foreach (var pair in base.AttributeExtensions)
            {
                writer.WriteAttributeString(pair.Key.Name, pair.Key.Namespace, pair.Value);
            }
            if (this.Rating != null)
            {
                this.Rating.WriteXml(writer);
            }
            if (this.Stats != null)
            {
                this.Stats.WriteXml(writer);
            }
            if (this.TagItems != null)
            {
                this.TagItems.WriteXml(writer);
            }
            foreach (var element in base.ElementExtensions)
            {
                element.WriteTo(writer);
            }
        }

        #endregion
    }
}